<?php

/**
 * @author @author Samrat Khan & Sadik Sarfaraz - Apr 30, 2013
 */
require 'foo.config.php';

if($_REQUEST['action'] == 'InCheck') {
    $serial = trim($_GET['InSerial']);
    
    $query = "SELECT *
                FROM stockin a
                WHERE stockin_id = (
                        SELECT MAX(stockin_id)
                         FROM stockin AS a WHERE a.serialno = '".$serial."'
                )";
    //$query = "SELECT *, MAX(inserteddate) FROM stockin WHERE serialno = '".$serial."' ";
    //var_dump($query);
    
    try {
        $stmt = $db->prepare($query);
        $stmt->execute();
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $rows = $stmt->fetchAll();
    echo json_encode($rows[0]);    
}


if($_REQUEST['action'] == 'Check') {
    $serial = trim($_GET['Serial']);
    
    $query = "SELECT *
                FROM stockin a
                WHERE stockin_id = (
                        SELECT MAX(stockin_id)
                         FROM stockin AS a WHERE a.serialno = '".$serial."'
                )";
    //$query = "SELECT *, MAX(inserteddate) FROM stockin WHERE serialno = '".$serial."' ";
    //var_dump($query);
    
    try {
        $stmt = $db->prepare($query);
        $stmt->execute();
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $rows = $stmt->fetchAll();
    echo json_encode($rows[0]);    
}

if($_REQUEST['action'] == 'FaultyCheck') {
    $serialF = trim($_GET['SerialF']);
    
    $query = "SELECT * FROM stockout a WHERE stockout_id = ( SELECT MAX(stockout_id) FROM stockout AS a WHERE a.serialno = '".$serialF."' )";
    //var_dump($query);
    
    try {
        $stmt = $db->prepare($query);
        $stmt->execute();
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $rows = $stmt->fetchAll();
    echo json_encode($rows[0]);    
}

if($_REQUEST['action'] == 'ResponseTime') {
    $responsetime = trim($_GET['responsetime']);
    $rows = getName('responsetype', 'hour', 'responsetype_id', $responsetime, $db);
    echo $rows;
}

if($_REQUEST['action'] == 'ExportCheck') {
    $serial = trim($_GET['ExportSerial']);
    
    $query = "SELECT * FROM faulties WHERE serialno = '".$serial."' ";
    
    try {
        $stmt = $db->prepare($query);
        $stmt->execute();
    } catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
    }
    $rows1 = $stmt->fetchAll();
    
    echo json_encode($rows1[0]);
}
?>
